<template>
    <el-form ref="formRef" style="max-width: 600px" :model="formData" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
         <el-form-item label="编号" prop="id">
                <el-input v-model="formData.id" />
            </el-form-item>
        <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="商品图片" prop="img">
            <Tuku v-model="formData.img"></Tuku>
        </el-form-item>
        <el-form-item label="商品排序" prop="seq">
            <el-input-number v-model="formData.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
             <el-input-number v-model="formData.price" :precision="2" :step="0.1"/>
            </el-form-item>
        <el-form-item label="类目父ID" prop="categoryId">
            <el-tree-select v-model="formData.categoryId" :data="categoryData" :render-after-expand="false"
                style="width: 240px" />
        </el-form-item>
        <el-form-item label="商品描述" prop="brief">
                    <!-- <el-input v-model="formData.brief" /> -->
                    <Editors v-model="formData.brief"></Editors>
                </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                提交
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { CategoryApi,ProductApi } from '@/api';
import Tuku from '@/components/Tuku.vue'
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router';
import Editors from '@/components/Editors.vue';
const router = useRouter()
const route=useRoute()
const formRef = ref()
const formData = reactive({
    id:0,
    name: '',
    img: '',
    seq: 0,
    categoryId: 0,
    price: 0,
    brief:''
})

const categoryData = ref([
    {
        value: 0,
        label: '一级目录'
    }

])
const rules = reactive({
    name: [
        { required: true, message: '请输入类目的名称', trigger: 'blur' },
        { min: 2, max: 10, message: '类目的名称必须是 最少2个 或 最大10字母', trigger: 'blur' },
    ],
})

onMounted(() => {
   // console.log(route.query.id);
    
     formData.id=Number(route.query.id)
    CallProductApi()
    CategoryTree()
})

//查询
const CallProductApi = () => { 
    ProductApi.select.call({id:formData.id}).then((res:any) => { 
       // console.log(res);
       // console.log(res[0].brief);
        
        formData.name = res[0].name
        formData.img = res[0].img
        formData.seq = res[0].seq
        formData.categoryId = res[0].categoryId
        formData.brief = res[0].brief
        formData.price = res[0].price
        
    })
}


const CategoryTree = () => {
    CategoryApi.select.call().then((categoryData1: any) => {
        categoryData1.forEach((item: any) => {
            item.value = item.id
            item.label = item.name
        });
        const categoryData2 = categoryData1.reduce((hezi: any, currentValue: any) => {
            //console.log("currentValue",currentValue);

            currentValue.children = categoryData1.filter((item: any) => {
                return item.parentId === currentValue.id
            })
            //判断父级的数据
            if (currentValue.parentId === 0) {
                hezi.push(currentValue)
            }
            return hezi;
        }, [])
        // console.log("盒子", categoryData2);

        categoryData.value = categoryData2
    })



}

const formsubmit = () => {

    ProductApi.update.call({
        id:formData.id,name: formData.name, img: formData.img, seq: formData.seq,
        price:formData.price,brief:formData.brief,categoryId: formData.categoryId
    }).then((res: any) => {
        console.log(res);
        ElMessage.success("恭喜你，修改成功")
        router.push({ name: 'product-list' })
    })
}


const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            formsubmit()
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style scoped></style>